package tomcat.connector;

import java.io.IOException;
import java.io.InputStream;

import static tomcat.Utils.sendRequest;

public class Request{
    private static final int BUFFER = 1024;

    private InputStream input;
    private String uri;

    public Request(InputStream input) {
        this.input = input;
    }

    public Request() {

    }

    public String getUri() {
        return uri;
    }

    public void parse() {
        int length = 0;
        byte[] buffer = new byte[BUFFER];
        try {
            length = input.read(buffer);
        } catch (IOException e) {
            e.printStackTrace();
        }
        StringBuilder request = new StringBuilder();
        for (int j = 0; j < length; j++) {
            request.append((char) buffer[j]);
        }

        uri = parseUri(request.toString());
    }

    private String parseUri(String s) {
        int index1, index2;
        index1 = s.indexOf(' ');
        if (index1 != -1) {
            index2 = s.indexOf(' ', index1 + 1);
            if(index2 > index1) {
                return s.substring(index1 + 1, index2);
            }
        }
        return "";
    }

    public static void main(String[] args) {
        String validRequest = "GET /index.html HTTP/1.1";
        sendRequest(validRequest);

    }


}
